<?php
  class AmiroLogParser{
      public  $path_to_log_file=""; // Symply path to log file 
      public  $array_lines=array(); // Array with lines (as like lines was getted via file() php functon)
      public static $log_colors=array("D4FF8A","62D04C","A1FD00","7CC202","68FF6F","FFFAAF","FCF361","FCED03","C3DAFF","74A8FE","9A9AFF");
      private $csv_output=false; //  Flag answer on questin (same or not to save csv_output)
      public $str_time_borders=""; // string for user with time segment that will be taken from the log

      
      /**
      * Open and set $array_lines via $path_to_log_file argument
      * Will be loaded only strings wich are withing time segmet setted by $left_time_border and $right_time_border
      * 
      * @param mixed $path_to_log_file
      * @param string $left_time_border sets the left border of the time segment we exploring, date must be in such format: "2011-08-02 10:15:53", if set false
      * there will be no left border
      * @param string $right_time_border sets the right border of the time segment we exploring, date maus be in such format: "2011-08-02 10:15:53", if set
      * false there will be no right border
      * @return AmiroLogParser
      */
      function AmiroLogParser($path_to_log_file,$left_time_border=false, $right_time_border=false){
          if(!is_file($path_to_log_file)||(!is_readable($path_to_log_file))){
            die("cannot read or find {$this->path_to_log_file}");
          }
          $this->path_to_log_file=$path_to_log_file;
          
          $res=fopen($this->path_to_log_file,"r");
          if(false===$res){
              die("cannot open {$this->path_to_log_file} by fopen");
          }
          
          // ----------- counting left and  right time borders timestamps        
          $left_timestamp=0;
          $right_timestamp=0;
          if(false!==$left_time_border){
                $match=array();
                preg_match("/((\d+)-(\d+)-(\d+) (\d+):(\d+):(\d+))/",$left_time_border,$match);
                $left_timestamp=mktime($match[5],$match[6],$match[7],$match[3],$match[4],$match[2]);
          }
          if(false!==$right_time_border){
                $match=array();
                preg_match("/((\d+)-(\d+)-(\d+) (\d+):(\d+):(\d+))/",$right_time_border,$match);
                $right_timestamp=mktime($match[5],$match[6],$match[7],$match[3],$match[4],$match[2]);
          }
          // ----------- counting left and  right time borders timestamps  
          // tmp make sure we get data
          $this->str_time_borders="Time borders left:{$left_time_border} right: {$right_time_border}";
          
          $new_line=""; // line that will be putted to the array_lines
          $new_line_create_time=""; // String from current line when it was created
          $new_line_timestamp=0;
          $match=array(); // array of matches for PREG REGEX
          
          // Getting new line from file, and save it to the array_lines array
          for(; (false!==($new_line=fgets($res)))  ;){
              // Get time when line was created 
              $match=array();
              preg_match("/\[((\d+)-(\d+)-(\d+) (\d+):(\d+):(\d+))\]/",$new_line,$match);
              // [0] => [2011-08-02 10:10:25]
              // [1] => 2011-08-02 10:10:25 All time
              // [2] => 2011 year
              // [3] => 08 month
              // [4] => 02 day
              // [5] => 10 hour
              // [6] => 10 minutes
              // [7] => 25 seconds
              // int mktime ([ int $hour = date("H") [, int $minute = date("i") [, int $second = date("s") [, int $month = date("n") [, int $day = date("j") [, int $year = date("Y") [, int $is_dst = -1 ]]]]]]] )
              $new_line_create_time=$match[1];
              $new_line_timestamp=mktime($match[5],$match[6],$match[7],$match[3],$match[4],$match[2]);
              
              // thanks this lind find a bug
              //echo $left_timestamp." ".($left_timestamp<$new_line_timestamp?"lt":"<b>gt</b>")." ".$new_line_timestamp." $new_line_create_time <br>";
              
              // if we do not reach left border we simply skip this line
              if(false!==$left_time_border){
                  if($left_timestamp>$new_line_timestamp){
                      continue;
                  }
              }
              // Saveing line at the array
              $this->array_lines[]=$new_line;
                            
              
              // if we reach and pass throught right border we do not need this lopp any more
              if(false!==$right_time_border){
                  if($right_timestamp<=$new_line_timestamp){
                      break;
                  }
              }
          }
          return true;
      }
      /**
       * Simple return one var
       */
      function GetFilePath(){
	      return $this->path_to_log_file;
      }
      /**
      * @return row html each line of the log file in the div
      * each line warped with div RowLine class
      */
      function GetLinesInDiv(){
          $ans="We start here:<br>";
          foreach($this->array_lines as $key=>$curr_line){
              $ans.="<div class=\"RowLine\">";
              $curr_line=preg_replace("/\[/","<div class=\"HtmlParseLeftIndent\">[",$curr_line);
              $curr_line=preg_replace("/\]/","]</div>",$curr_line);
              $ans.=$curr_line;
              $ans.="</div>";
          }
          return $ans;
      }
      /**
      *  Returinng CSV text
      * also saving it at the $this->csv_output
      * 
      */
      function GetCSV_String(){
          $ans="";
          foreach($this->array_lines as $key=>$curr_line){
          // First row; Here we saving time
              $match=array();
              preg_match("/\[(\d+-\d+-\d+ \d+:\d+:\d+)\]/",$curr_line,$match);
              //$ans.=print_r($match,true).";";
	          $ans.=$match[1].";";

	      // Second Row; will be the page that had been called (Request_URI)
              $match=array();
              preg_match("/REQUEST_URI=\(([^)]*)\)/",$curr_line,$match);
              //$ans.=print_r($match,true).";";
	          $ans.=$match[1].";";
              
          // Third row is a peak of memory usage
            $match=array();
              preg_match("/peak: (\d*)\.*(\d*)/",$curr_line,$match);
              //$ans.=print_r($match,true).";";
              $ans.=$match[1].(($match[2]!="")?",".$match[2]:"").";"; //  cuz off excele
              
              $ans.="\r\n";
          }
          $this->csv_output=$ans;
          return $ans;
      }
      /**
      * Saving CSV to frim from $this->csv_output
      * 
      * @param mixed $file_name for sevain csv
      * @return bool
      */
      function SaveCSV_file($file_name){
          if(false===$this->csv_output) return false;
          $ans=file_put_contents($file_name,$this->csv_output);
          return $ans?true:false;
      }
      

      
  
  }
?>
